package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.DetachCurrentUserFromGroupEvent;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.ShowDialogMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.ShowSelectGroupEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.DetachCurrentUserFromGroupListener;
import ua.org.nuos.sdms.clientgui.client.listeners.DialogResultHandler;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.util.exception.UpdateEntityException;
import ua.org.nuos.sdms.middle.util.exception.OneGroupLeaderException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 07.04.12
 * Time: 22:27
 * To change this template use File | Settings | File Templates.
 */
public class DetachCurrentUserFromGroupHandler implements DetachCurrentUserFromGroupListener {
    @Override
    public void detach(DetachCurrentUserFromGroupEvent event) {
        ShowDialogMessageEvent e = new ShowDialogMessageEvent(getDialogResultHandler(), "Внимание!", "<b>Выйти из группы?</b>");
        AppProxy.getInstance().getController(GlobalBoard.ID).fire(e);
    }

    private DialogResultHandler getDialogResultHandler() {
        return new DialogResultHandler() {
            @Override
            public void dialogResultProcess(boolean accepted) {
                if (accepted) {
                    Group group = AppProxy.getInstance().getModelLocator().getGroup();
                    try {
                        if (group != null) {
                            AppProxy.getInstance().getServiceLocator().getClientBean().detachCurrentUserFromGroup(group.getId());
                            AppProxy.getInstance().getModelLocator().setGroup(null);
                        }
                        AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ShowSelectGroupEvent());
                    } catch (OneGroupLeaderException e) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Detach user from group error: user is last group leader", e);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(
                                new ErrorMessageEvent(String.format(Messages.DETACH_USER_FROM_GROUP_ONE_LEADER_ERROR, group.getName())));
                    } catch (UpdateEntityException e) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Detach user from group error: user is last group leader", e);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.UPDATE_GROUP_ERROR));
                    } catch (Exception ex) {
                        Logger.getAnonymousLogger().log(Level.WARNING, "Detach user from group error", ex);
                        AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
                    }
                }
            }
        };
    }
}
